from flask import logging
from transformers import pipeline

def text_to_sql(natural_language_query):
    try:
        # Instantiate the LLM with specific parameters (e.g., model size)
        llm = pipeline("text2sql", model="gpt-4", temperature=0.5)

        # Prompt for converting natural language to SQL
        prompt = f"Translate this natural language query to SQL: '{natural_language_query}'"

        # Generate SQL query
        sql_query = llm(prompt, max_length=100)

        logging.info(f"Generated SQL query: {sql_query}")
        return sql_query

    except Exception as e:
        logging.error(f"Error generating SQL query: {e}")
        return None

# Example usage
natural_language_query = "Show me all orders from January 2021"
sql_query = text_to_sql(natural_language_query)

if sql_query:
    print(sql_query)
else:
    print("Failed to generate SQL query.")